<?php

require_once  __DIR__ . '/includes/authenticate.php';

include_once __DIR__ . '/db_bb_connect.php';

require_once __DIR__ . '/includes/BasketbalFunctions.php';

include __DIR__ . '/mainpage.php';

$basketbalfunctions = new BasketbalFunctions();

?>
<div id="container-fluid">
	    <div class="row">
     <div class="col-md-8 col-md-offset-2" >
          <h3 class="text-center">Other Video Archive</h3>
     </div>
        </div>
	<div class="row">
		<div class="col-md-6 col-md-offset-3">
			<table class="table table-hover table-striped table-condensed">
		    <thead>
		   </thead>
		   <tbody>
		   	<?php

		   	$sql = "SELECT * FROM video_other WHERE archived = 'yes' ORDER BY date_create DESC";

		   	if ($db->query($sql)) {  
						$result = $db->query($sql);
						while ($row = $result->fetch_assoc()) {
							$ID = $row['ID'];
							$embed_code = $row['embed_code'];
							$title = $row['title'];
							$comment = nl2br($row['comment']);

		   	?>
			   	<tr id="tablerow<?= $ID?>"> 
			   		<td><div id="player<?= $ID?>" align='center'> <button class="btn btn-sm btn-default loadvideo<?= $ID?>">Load Video</button></td>
			   		<td id="bg<?= $ID?>">
			   			<strong>Title:</strong> <?=$title?> 
			   		<br>
			   		<strong>Visibility: </strong>

					<?php 
					$visibility = $basketbalfunctions->ShowVideoOtherVisibilityNamelist($ID); 
						
					echo $visibility;							
					if ( $visibility != 'nobody') {
						
						?>
						<script type="text/javascript">
							document.getElementById("bg<?= $ID?>").style.backgroundColor = "#dde5d0"; 
						</script>
						<?php
					} else {?>

															<script type="text/javascript">
							document.getElementById("bg<?= $ID?>").style.backgroundColor = "#ffe6e6"; 
						</script>
						<?php
						
					}


					?> <br>
			   			<strong>Comment:</strong> <?=$comment?> 
			   		</td>
					<td> 
						<form> 
						<input type="hidden" name="ID" value="<?=$ID?>"/>
						<button class="btn btn-sm btn-warning unarchive_entry" title="Unarchive">Unarchive</button>
						</form>
					</td>

				</tr>	
			<?php }}?>		   	
		   </tbody>
		</table>
		</div>
	</div>

</div>

<script type="text/javascript">

var tag = document.createElement('script');

tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

      <?php 

      $result = $db->query($sql);

      while ($row = $result->fetch_assoc()) { 

      	$ID = $row['ID'];

       ?>
      var player<?= $ID?>;
      var clicked<?= $ID?> = false;	

      <?php }    

      $result = $db->query($sql);

      while ($row = $result->fetch_assoc()) { 

		$embed_code = $row['embed_code'];
		$ID = $row['ID'];

       ?>

       $('.loadvideo<?= $ID?>').on("click", function(){

	       function onYouTubeIframeAPIReady() {
				player<?= $ID?> = new YT.Player('player<?= $ID?>', {
	          	height: '150',
	          	width: '250',
	          	videoId: '<?= $embed_code?>'
		    });

        	}

        	onYouTubeIframeAPIReady(); 

    	}); 

<?php } ?>

			$(".unarchive_entry").on("click", function(e) {
			    postData = $(this.form).serialize();
			    var ID = $(this.form).find('input[name="ID"]').val();
			    if( confirm("Are you sure to unarchive this video?")){
				    	$.ajax({
				      type: "POST",
				        url: "./includes/handlers/video_other_unarchive_entry_handler.php",
				        data: postData,
				        success: function(data) {    
				          $("#tablerow" + ID).html(data);
				        },
				        error: function (xhr, status, error) {
				          $("#tablerow" + ID).html(xhr.responseText);
				        }
				      }); 
				   }
				    e.preventDefault();
			});

</script>